<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/tcms_registry_keys.class.php');
require_once($WSC->get_site_inc_dir().'modules/registry_db/registry_key_db.class.php');


class TCMS_Settings_Dir_Index_DBC extends Tangra_Class implements I_DB_Storable {
	private $id;
	private $switch;
	private $files;

	//fake
	public function set_id($id) {
	}


	//fake
	public function get_id() {
		return 1;
	}


	public function save(DB_Connection $dbc) {
		$this->switch->save($dbc);
		$this->files->save($dbc);

		return 1;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);


		$switch_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_DIR_INDEX_SWITCH);
		if ($switch_id) {
			$switch = new Registry_Key_DB();
			$switch->load_by_id($dbc, $switch_id);
			$this->switch = $switch;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_DIR_INDEX_SWITCH);
		}

		$files_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_DIR_INDEX_FILES);
		if ($files_id) {
			$files = new Registry_Key_DB();
			$files->load_by_id($dbc, $files_id);
			$this->files = $files;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_DIR_INDEX_FILES);
		}

		return 1;
	}


	public function set_switch($value) {
		$this->switch->set_value($value ? 1 : 0);
	}


	public function get_switch() {
		return $this->switch->get_value();
	}


	public function set_files($value) {
		$this->files->set_value($value);
	}


	public function get_files() {
		return $this->files->get_value();
	}


	public function get_files_arr() {
		$ret = explode(',', $this->files->get_value());
		foreach($ret as &$file) {
			$file = trim($file);
		}

		return $ret;
	}
}